package com.blueheart.music.service.impl;

import com.blueheart.music.exception.BaseException;
import com.blueheart.music.exception.BaseExceptionEnum;
import com.blueheart.music.mapper.SongMapper;
import com.blueheart.music.model.pojo.Song;
import com.blueheart.music.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SongServiceImpl implements SongService {
    @Autowired
    private SongMapper songMapper;
    /**
     * 增
     *
     * @param song
     */
    @Override
    public Boolean addSong(Song song) {
        return songMapper.insertSelective(song)>0;
    }

    /**
     * 删
     *
     * @param id
     */
    @Override
    public Boolean removeSong(Integer id) {
        return songMapper.deleteByPrimaryKey(id)>0;
    }

    /**
     * 改
     *
     * @param song
     */
    @Override
    public Boolean reviseSong(Song song) {
       return songMapper.updateByPrimaryKeySelective(song)>0;
    }

    /**
     * 查询全部
     */
    @Override
    public List<Song> findAll() {
        return songMapper.selectAll();
    }

    /**
     * 歌名模糊查询歌手
     *
     * @param name
     */
    @Override
    public List<Song> findByName(String name) {
        return songMapper.selectByName(name);
    }

    /**
     * 歌手id查询歌手
     *
     * @param id
     */
    @Override
    public List<Song> findBySingerId(Integer id) {
        return songMapper.selectById(id);
    }

    /**
     * 主键id查询歌手
     *
     * @param id
     */
    @Override
    public Song findById(Integer id) {
        return songMapper.selectByPrimaryKey(id);
    }

    /**
     * 重名判断
     *
     * @param name
     */
    @Override
    public Song checkName(String name) {
        Song song = songMapper.selectName(name);
        if (song!=null){
            throw  new BaseException(BaseExceptionEnum.SONG_EXIST);
        }
        return song;
    }

    /**
     * 查询主键
     *
     * @param name
     * @return
     */
    @Override
    public Song findToId(String name) {
        return songMapper.selectName(name);
    }

}

